define(function(require, exports, module) {
	"require:nomunge,exports:nomunge,module:nomunge";
	
    var self = exports;
    var utils = require('mokit/utils');
    var service = require('./service');
    var Ex = require("./Ex");
    var user = exports.user =  require('./User');
	
	self.nav = require("mokit/navigation");

    self.createProject = function(type) {
        self.theProject = {
            projectType: type,
            projectName: '',
            projectInfo: '',
            position: []
        };
        return self.theProject;
    };
    self.createProject('0');

    self.submitProject = function(callback) {
        var info = self.theProject;
        if (info.projectName == '' || info.projectInfo == '') {
            if (callback) callback('项目需求信息不完整');
            return;
        }
        if (info.position == null || info.position.length < 1) {
            if (callback) callback('请至少添加一个需求人员');
            return;
        }
        service.project.submit(info, function(rs, err) {
            if (err && callback) return callback(err.message || err);
            if (rs.success) {
                if (callback) callback();
            } else {
                rs = rs || {};
                var msg = rs.msg || 'unknow error';
                if (callback) callback(msg);
            }
        });
    };

    self.deletePositionByIndex = function(index) {
        var buffer = [];
        utils.each(self.theProject.position, function(i, item) {
            if (i !== index) {
                buffer.push(item);
            }
        });
        self.theProject.position = buffer;
    };

    self.addPosition = function(info, callback) {
        if (info.roleName == '' || info.remuneration == '' || info.timeLimit == '' || info.requirement == '') {
            if (callback) callback('需求人员信息不完整');
            return;
        }
        self.theProject.position.push(info);
        if (callback) callback();
    };



	/* add by Li at 2015.3.27 */
	
	self.imgPath = user.syproUri;
	
	
	/*============项目类别=========*/
	
	self.catList = [];		//项目类别列表
	self.catMap = {};		//项目类别映射
	
	//映射类别
    self.mapCat = function(catList) {	
	    	catList.forEach(function(item){
	    		self.catMap[item.categoryCode] = item.categoryName;
	    	});
    };
     
    //获取项目类别列表
    self.getCatList = function(callback){
    		if(this.catList.length>0){
    			return callback();
    		}
        service.project.getCatList(function(rs, err) {
            if (err && callback) return callback(err.message || err);
            if (rs.success) {
               	self.catList = rs.obj;
               	self.mapCat(self.catList);
                if (callback) callback();
            } else {
                rs = rs || {};
                var msg = rs.msg || 'unknow error';
                if (callback) callback(msg);
            }
        });    	
    }; 
    
	/*============项目列表=========*/   
	
	self.list = [];			//项目列表

	//项目列表参数
	self.args = {
		style:"image",
		serviceType:"sel",
		jingbiaoday:"sel",
		biddingType:"sel",
		authentication:"sel",
		page:1, 
		pageSize:10
	};
	self.sortBy = "timeLimit";
	self.sortDir = "desc"; 

	//重置列表
	self.resetList = function(){
		this.list = [];
		this.args.page = 1;
		this.isEnd = false;
	};
	
     //初始化列表
	self.iniList = function(cat, callback){
    	this.resetList();
		this.args.serviceType = cat;
     	this.getList(callback);
	};
	
    //排序列表
    self.sortList = function(sortBy, callback){
     	this.sortDir = this.sortBy==sortBy ? (this.sortDir=="asc"?"desc":"asc") : "desc";
     	this.sortBy = sortBy;
		this.iniList(this.args.serviceType, callback);
    };

    //加载更多列表
	self.loadMoreList = function(callback){
		if(!this.isEnd){
			this.args.page++;
			this.getList(callback);
		}
	};
	
	//获取某个类型下的项目列表
	self.getList = function(callback){
     	var args = JSON.parse(JSON.stringify(this.args));
     	args[this.sortBy] = this.sortDir;
        service.project.getProjectList(args, function(rs, err) {
            if (err && callback) return callback(err.message || err);
            if (rs.success) {
               	self.list = self.list.concat(rs.obj);
               	self.isEnd = rs.obj.length<self.args.pageSize;
               	var isEmpty = self.args.page>1 && rs.obj.length==0;
                if (callback) callback(!isEmpty);
            } else {
                rs = rs || {};
                var msg = rs.msg || 'unknow error';
                if (callback) callback(msg);
            }
        });
	};

	/*============项目详情=========*/ 
	
	self.projectRoleId = 0;
   	self.info = {
   		projectVo: {},
   		projectRoleVo: {},
   		commentList: []
   	};
   	
    //获取项目详情
     self.getInfo = function(id, callback){
     	self.projectRoleId = id;
        service.project.getProjectInfo({projectRoleId:id}, function(rs, err) {
            if (err && callback) return callback(err.message || err);
            if (rs.success) {
               	self.info = rs.obj;
                if (callback) callback();
            } else {
                rs = rs || {};
                var msg = rs.msg || 'unknow error';
                if (callback) callback(msg);
            }
        });    	
    };
    
    //添加收藏
    self.addCollection = function(prid, callback){
    	Ex.addCollection(prid, 1, callback);
    };
 
 
 	self.joinArgs = {	
		projectId:0,
		roleId:0,
		myOffer:0,
		plannedSchedule:0,
		myPlan: "44455",
		userId: user.state.userId
	};
	
     //加入项目
    self.join2project = function(callback){
    	self.joinArgs.projectId = self.info.projectVo.id;
    	self.joinArgs.roleId = self.projectRoleId;
        service.project.join2project(self.joinArgs, function(rs, err) {
            if (err && callback) return callback(err.message || err);
            if (rs.success) {
                if (callback) callback(rs.msg);
            } else {
                rs = rs || {};
                var msg = rs.msg || 'unknow error';
                if (callback) callback(msg);
            }
        });
    };
    

	/*============职位详情=========*/ 
	
	self.projectId = 0;
	self.plist = {};
   	self.pinfo = {};
   	
    //获取职位详情
     self.getPosInfo = function(prid, pid, callback){
        service.project.getPosInfo({projectRoleId:prid,projectId:pid}, function(rs, err) {
            if (err && callback) return callback(err.message || err);
            if (rs.success) {
               	self.plist = rs.obj;
                if (callback) callback();
            } else {
                rs = rs || {};
                var msg = rs.msg || 'unknow error';
                if (callback) callback(msg);
            }
        });    	
    };

});